package com.pzhu.servlet;

import com.pzhu.controller.BookController;
import com.pzhu.controller.UserController;
import com.pzhu.controller.impl.BookControllerImpl;
import com.pzhu.controller.impl.UserControllerImpl;
import com.pzhu.pojo.User;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ShowOnlineUser extends HttpServlet {
    private UserController userController = new UserControllerImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        if (session.getAttribute("login") == null) {
            req.getRequestDispatcher("/main").forward(req, resp);
            return;
        }
        ServletContext context = this.getServletContext();
        List<Long> userIdList = (List<Long>) context.getAttribute("UserIdList");
        ArrayList<User> users = new ArrayList<>();
        for (Long id :
                userIdList) {
            User user = userController.selectUser(id);
            users.add(user);
        }
        req.setAttribute("userList", users);
    }
}
